<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Payment_model extends CI_Model {

	function __construct() {
		parent::__construct();

		$this->load->library('LogSql');
	}
	
	/**
	 * 이미 결제 처리가 되었는지 결제내역 확인
	 */
	function is_exist_payment_history($ordno) {
		log_message('debug',"#### Payment_model::is_exist_payment_history($ordno)");
		
		$query = $this->db->get_where('tb_payment_history',array('ordno'=>$ordno)) or $this->logsql->log_sql('error');
		$result = $query->row_array();

		$this->logsql->log_sql();
		
		if (count($result) > 0) {
			return TRUE;
		}
		return FALSE;
	}

	/**
	 * 페이레터 결제 내역 저장
	 */
	function insert_payment_history($return_param) {
		log_message('debug',"#### Payment_model::insert_payment_history($return_param)");
		
		$arr_pgcode = array(
			'1'=>'신용카드일',
			'2'=>'계좌이체',
			'3'=>'휴대폰',
			'4'=>'ARS',
			'5'=>'문화상품권',
			'6'=>'도서문화상품',
			'7'=>'가상계좌',
			'8'=>'휴대폰',
			'9'=>'Cyber1588',
			'10'=>'해피머니상품권',
			'11'=>'온캐시',
			'12'=>'게임문화상품권',
			'13'=>'틴캐시',
			'14'=>'BUYFORME',
			'15'=>'신용카드',
			'16'=>'가상계좌',
			'17'=>'편의점 결제',
			'18'=>'ARS'
		);

		$this->db->set('user_id'    , $return_param['userid']);
		$this->db->set('ordnm'      , $return_param['ordnm']);
		$this->db->set('ordno'      , $return_param['ordno']);
		$this->db->set('svcnm'      , $return_param['svcnm']);
		$this->db->set('pname'      , $return_param['pname']);
		$this->db->set('ptype'      , $return_param['ptype']);
		$this->db->set('pcode'      , $return_param['pcode']);
		$this->db->set('pgcode'     , $arr_pgcode[$return_param['pgcode']]);
		$this->db->set('pltid'      , $return_param['pltid']);
		$this->db->set('tid'        , $return_param['tid']);
		$this->db->set('cid'        , $return_param['cid']);
		$this->db->set('amt'        , $return_param['amt']);
		$this->db->set('txdate'     , $return_param['txdate']);
		$this->db->set('txtime'     , $return_param['txtime']);
		$this->db->set('cardname'   , $return_param['cardname']);
		$this->db->set('accountno'  , $return_param['accountno']);
		$this->db->set('accountname', $return_param['accountname']);
		$this->db->set('expymd'     , $return_param['expymd']);
		$this->db->set('create_dt'  , 'now()', FALSE);
		$this->db->insert('tb_payment_history') or $this->logsql->log_sql('error');

		$this->logsql->log_sql();
	}

	/**
	 * 실적별 회원등급 업데이트
	 */
	function member_class_up($user_id) {
	 	log_message('debug',"#### Payment_model::member_class_up($user_id)");
		
		// 실적합계
		$total_payment = $this->get_total_payment($user_id);
		
		// 실적별 회원등급 리스트
		$class_list = $this->select_member_class_code_list();
		
		// 해당 등급 찾기
		$class_cd = '';
		foreach ($class_list as $item) {
			if ($item['class_cash'] <= $total_payment) {
				$class_cd = $item['class_cd'];
			} else {
				break;
			}
		}
		// 회원등급 업데이트
		$this->update_member_class($user_id, $class_cd);
	}

	/**
	 * 회원의 실적 합계
	 */
	function get_total_payment($user_id) {
		log_message('debug',"#### Payment_model::get_total_payment($user_id)");
		
		$sql  = PHP_EOL." SELECT SUM( price ) total_payment ";
		$sql .= PHP_EOL." FROM tb_point_charged_history ";
		$sql .= PHP_EOL." WHERE user_id = ? ";
		$sql .= PHP_EOL." AND   point_type = 'C' ";
		$query = $this->db->query($sql, array($user_id));
		$result = $query->row_array();
		$total_payment = $result['total_payment'];	// 총 결제 금액

		$this->logsql->log_sql();
		$this->logsql->log_result($result);
		
		return $total_payment;
	}

	/**
	 * 실적별 회원등급 리스트
	 */
	function select_member_class_code_list() {
		log_message('debug',"#### Payment_model::select_member_class_code_list()");
		
		// 실적별 회원등급 리스트를 가져온다.
		$this->db->order_by('class_cash','ASC');
		$query = $this->db->get('tb_member_class_code');
		$result = $query->result_array();

		$this->logsql->log_sql();
		$this->logsql->log_result($result);
		
		return $result;
	}
	
	/**
	 * 회원등급 업데이트
	 */
	function update_member_class($user_id, $class_cd) {
		log_message('debug',"#### Payment_model::update_member_class($user_id, $class_cd)");
		
		$this->db->set('class_cd' , $class_cd);
		$this->db->set('update_dt', 'now()', FALSE);
		$this->db->where('user_id', $user_id);
		$this->db->where('use_yn', 'Y');
		$this->db->update('tb_member') or $this->logsql->log_sql('error');
				
		$this->logsql->log_sql();
	}
}

/* End of file Payment_model.php */
/* Location: ./application/models/payment/Payment_model.php */